package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Travel;
import cn.wolfcode.trip.base.domain.TravelContent;
import cn.wolfcode.trip.base.mapper.TravelContentMapper;
import cn.wolfcode.trip.base.mapper.TravelMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.ITravelService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class TravelServiceImpl implements ITravelService {

    @Autowired
    private TravelMapper travelMapper;
    @Autowired
    private TravelContentMapper contentMapper;


    @Override
    public int deleteByPrimaryKey(Long id) {
        contentMapper.deleteByPrimaryKey(id);
        travelMapper.deleteRelation(id);
        return travelMapper.deleteByPrimaryKey(id);
    }


    @Override
    public Travel selectByPrimaryKey(Long id) {
        Travel travel = travelMapper.selectByPrimaryKey(id);
        TravelContent travelContent = contentMapper.selectByPrimaryKey(id);
        travel.setTravelContent(travelContent);
        return travel;
    }

    @Override
    public List<Travel> selectAll() {
        return travelMapper.selectAll();
    }

    @Override
    public void saveOrUpdate(Travel travel) {
        if (travel.getId() == null) {
            //创建时间
            travel.setCreateTime(new Date());
            //编辑时间
            travel.setLastUpdateTime(new Date());
            //保存游记之后才有ID
            travelMapper.insert(travel);
            //获取这个文章
            TravelContent travelContent = travel.getTravelContent();
            travelContent.setId(travel.getId());
            //新增文章
            contentMapper.insert(travelContent);
        } else {
            TravelContent travelContent = contentMapper.selectByPrimaryKey(travel.getId());
            if (travelContent == null) {
               TravelContent t= travel.getTravelContent();
               t.setId(travel.getId());
                contentMapper.insert(t);
            }
            travelMapper.updateByPrimaryKey(travel);
        }

    }


    @Override
    public PageInfo getList(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<Travel> list = travelMapper.getList(qo);
        return new PageInfo(list);
    }

    @Override
    public void changeState(Travel travel) {
        if (travel.getState()==Travel.STATE_PASS){
        travel.setReleaseTime(new Date());
        }
        travelMapper.changeState(travel);
    }

    @Override
    public Travel getContent(Long id) {
        Travel travel = travelMapper.selectByPrimaryKey(id);
        TravelContent travelContent = contentMapper.selectByPrimaryKey(id);
        travel.setTravelContent(travelContent);
        return travel;
    }

    @Override
    public PageInfo selectTravelByStrategyId(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<Travel> list = travelMapper.selectTravelByStrategyId(qo);
        return new PageInfo(list);
    }

    @Override
    public void deleteRelation(Long id) {
        travelMapper.deleteRelation(id);
    }

    @Override
    public void insertRelation(Long strategyId, Long travelId) {
        travelMapper.insertRelation(strategyId,travelId);
    }

    @Override
    public List<Travel> selectTravelByMid() {
        return travelMapper.selectTraveByMid();
    }

    @Override
    public PageInfo getTravels(QueryObject qo) {

        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<Travel> list = travelMapper.getTravels(qo);
        return new PageInfo(list);
    }

    @Override
    public int getTravelCount(Long authorId) {
        return travelMapper.selectCount(authorId);
    }

    @Override
    public List<Travel> selectByKeyword(QueryObject qo) {
        return travelMapper.selectByKeyword(qo);
    }


}
